
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/18/2012 14:47:00
-- Generated from EDMX file: E:\TeamProject\HNQS\ChatApp\SourceCode\ChatApp.Service\Model\EntityFramework.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [DBChat];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_AddFriend_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[AddFriends] DROP CONSTRAINT [FK_AddFriend_User];
GO
IF OBJECT_ID(N'[dbo].[FK_AddFriend_User1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[AddFriends] DROP CONSTRAINT [FK_AddFriend_User1];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_Room]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Message_Room];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Message_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_User1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Message_User1];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[AddFriends]', 'U') IS NOT NULL
    DROP TABLE [dbo].[AddFriends];
GO
IF OBJECT_ID(N'[dbo].[Messages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Messages];
GO
IF OBJECT_ID(N'[dbo].[Rooms]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Rooms];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'AddFriends'
CREATE TABLE [dbo].[AddFriends] (
    [IdAddFriend] int IDENTITY(1,1) NOT NULL,
    [IdUser] int  NOT NULL,
    [IdUserAdd] int  NOT NULL,
    [NgayTao] datetime  NOT NULL
);
GO

-- Creating table 'Messages'
CREATE TABLE [dbo].[Messages] (
    [IdMessage] int IDENTITY(1,1) NOT NULL,
    [IdUser] int  NOT NULL,
    [IdUserReceive] int  NOT NULL,
    [Content] varchar(max)  NULL,
    [IdRoom] int  NOT NULL,
    [NgayTao] datetime  NOT NULL,
    [IsRead] bit  NOT NULL
);
GO

-- Creating table 'Rooms'
CREATE TABLE [dbo].[Rooms] (
    [IdRoom] int IDENTITY(1,1) NOT NULL,
    [NgayTao] datetime  NOT NULL,
    [LaChatRoom] bit  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [IdUser] int IDENTITY(1,1) NOT NULL,
    [UserName] nvarchar(50)  NOT NULL,
    [PassWord] varchar(max)  NOT NULL,
    [Email] varchar(50)  NULL,
    [NgayTao] datetime  NOT NULL,
    [TrangThai] tinyint  NOT NULL,
    [KichHoat] bit  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [IdAddFriend] in table 'AddFriends'
ALTER TABLE [dbo].[AddFriends]
ADD CONSTRAINT [PK_AddFriends]
    PRIMARY KEY CLUSTERED ([IdAddFriend] ASC);
GO

-- Creating primary key on [IdMessage] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [PK_Messages]
    PRIMARY KEY CLUSTERED ([IdMessage] ASC);
GO

-- Creating primary key on [IdRoom] in table 'Rooms'
ALTER TABLE [dbo].[Rooms]
ADD CONSTRAINT [PK_Rooms]
    PRIMARY KEY CLUSTERED ([IdRoom] ASC);
GO

-- Creating primary key on [IdUser] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([IdUser] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [IdUser] in table 'AddFriends'
ALTER TABLE [dbo].[AddFriends]
ADD CONSTRAINT [FK_AddFriend_User]
    FOREIGN KEY ([IdUser])
    REFERENCES [dbo].[Users]
        ([IdUser])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AddFriend_User'
CREATE INDEX [IX_FK_AddFriend_User]
ON [dbo].[AddFriends]
    ([IdUser]);
GO

-- Creating foreign key on [IdUserAdd] in table 'AddFriends'
ALTER TABLE [dbo].[AddFriends]
ADD CONSTRAINT [FK_AddFriend_User1]
    FOREIGN KEY ([IdUserAdd])
    REFERENCES [dbo].[Users]
        ([IdUser])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AddFriend_User1'
CREATE INDEX [IX_FK_AddFriend_User1]
ON [dbo].[AddFriends]
    ([IdUserAdd]);
GO

-- Creating foreign key on [IdRoom] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Message_Room]
    FOREIGN KEY ([IdRoom])
    REFERENCES [dbo].[Rooms]
        ([IdRoom])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_Room'
CREATE INDEX [IX_FK_Message_Room]
ON [dbo].[Messages]
    ([IdRoom]);
GO

-- Creating foreign key on [IdUser] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Message_User]
    FOREIGN KEY ([IdUser])
    REFERENCES [dbo].[Users]
        ([IdUser])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_User'
CREATE INDEX [IX_FK_Message_User]
ON [dbo].[Messages]
    ([IdUser]);
GO

-- Creating foreign key on [IdUserReceive] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Message_User1]
    FOREIGN KEY ([IdUserReceive])
    REFERENCES [dbo].[Users]
        ([IdUser])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_User1'
CREATE INDEX [IX_FK_Message_User1]
ON [dbo].[Messages]
    ([IdUserReceive]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------